<?php


namespace App\Repositories;


use App\Exceptions\BasicException;

class ConnectPrizePoolRepository
{
    //添加或编辑人脉奖池
    public function addOrEdit($param)
    {
        $id = $param['id'] ?? 0;
        $prizePoolName = $param['prize_pool_name'];

        $exist = app('model_connect_prize_pool')
            ->where('name', $prizePoolName)
            ->where(function ($query) use ($id) {
                if ($id > 0) {
                    $query->where('id', '<>', $id);
                }
            })->value('id');
        if ($exist) throw new BasicException(0, '奖池名称已经存在');

        $data = ['name' => $prizePoolName];
        if ($id > 0) {
            $prizePool = app('model_connect_prize_pool')->select('id')->where('id', $id)->first();
            if (empty($prizePool)) throw new BasicException(0, 'id错误，记录不存在');

            $res = app('model_connect_prize_pool')->where('id', $id)->update($data);
        } else {
            $res = app('model_connect_prize_pool')->insert($data);
        }
        return $res;
    }

    //添加或编辑人脉奖池道具
    public function addOrEditProp($param)
    {
        $id = $param['id'] ?? 0;
        $prizePoolId = $param['prize_pool_id'];
        $prizePoolType = $param['prize_pool_type'];
        $propType = $param['prop_type'];
        $propId = $param['prop_id'];
        $propNum = $param['prop_num'];
        $useDays = $param['use_days'] ?? 0;

        $prizePool = app('model_connect_prize_pool')->where('id', $prizePoolId)->value('id');
        if (empty($prizePool)) throw new BasicException(0, '奖池id错误，奖池不存在');

        switch ($propType) {
            case 1:
                $gift = app('model_gift')->where('id', $propId)->where('sold', 1)->first();
                if (empty($gift)) throw new BasicException(0, '礼物不存在或已下架');
                break;
            case 2:
                $product = app('model_mall_products')->where('id', $propId)->first();
                if (empty($product)) throw new BasicException(0, '商品不存在或已下架');
                break;
        }

        $data = [
            'prize_pool_id' => $prizePoolId,
            'prop_type' => $propType,
            'prop_id' => $propId,
            'init_stock' => $propNum,
        ];
        if ($propType == 2) {
            $data['valid_period'] = $useDays;
        }
        switch ($prizePoolType) {
            case 1:
                $model = app('model_connect_prize_pool_prop');
                $data['remaining_stock'] = $propNum;
                break;
            case 2:
                $model = app('model_connect_prize_pool_prop_next');
                break;
        }

        $prizePoolProp = $model
            ->where('prize_pool_id', $prizePoolId)
            ->where('prop_type', $propType)
            ->where('prop_id', $propId)
            ->where(function ($query) use ($id) {
                if ($id > 0) {
                    $query->where('id', '<>', $id);
                }
            })
            ->where('status', 1)
            ->value('id');
        if ($prizePoolProp) throw new BasicException(0, '奖池已存在该道具');

        if ($id > 0) {
            $prizePoolProp = $model->select('id')->where('id', $id)->first();
            if (empty($prizePoolProp)) throw new BasicException(0, 'id错误，记录不存在');

            $res = $model->where('id', $id)->update($data);
        } else {
            $res = $model->insert($data);
        }
        return $res;
    }
}